package cn.edu.hyit.hjq.volunteems.backend.service.impl;

import cn.edu.hyit.hjq.volunteems.backend.mapper.PointRecordMapper;
import cn.edu.hyit.hjq.volunteems.backend.pojo.PointRecord;
import cn.edu.hyit.hjq.volunteems.backend.service.PointRecordService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 积分记录服务实现类
 *
 * @module 积分服务
 */
@Service
public class PointRecordServiceImpl implements PointRecordService {

    private final PointRecordMapper pointRecordMapper;

    public PointRecordServiceImpl(PointRecordMapper pointRecordMapper) {
        this.pointRecordMapper = pointRecordMapper;
    }

    @Override
    public List<PointRecord> listPointRecords(String userId, Integer page, Integer size) {
        // 使用 PageHelper 实现分页查询
        PageHelper.startPage(page, size);
        return pointRecordMapper.findByUserId(userId);
    }

    @Override
    public PointRecord getPointRecordDetail(Long id) {
        return pointRecordMapper.findById(id);
    }

    @Override
    public int getTotalPointsByVolunteerId(String volunteerId) {
        Integer sum = pointRecordMapper.sumPointsByVolunteerId(volunteerId);
        return sum != null ? sum : 0;
    }


    @Override
    public void addPointRecord(PointRecord pointRecord) {
        pointRecordMapper.save(pointRecord);
    }

    @Override
    public void updatePointRecord(Long id, PointRecord pointRecord) {
        pointRecord.setRecordId(id);
        pointRecordMapper.update(pointRecord);
    }

    @Override
    public void deletePointRecord(Long id) {
        pointRecordMapper.deleteById(id);
    }
}
